unit EdListaCompra;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxDBLookupComboBox, cxCurrencyEdit,
  FMTBcd, cxLookAndFeelPainters, StdCtrls, cxButtons, ExtCtrls, DBClient,
  Provider, SqlExpr, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  cxDropDownEdit, cxLookupEdit, cxDBLookupEdit, cxDBEdit, cxTextEdit,
  cxMaskEdit, cxCalendar, cxLabel, cxDBLabel, cxContainer, cxGroupBox;

type
  TfrmEdListaCompra = class(TForm)
    cxGroupBox2: TcxGroupBox;
    lblNumero: TcxDBLabel;
    cxGroupBox4: TcxGroupBox;
    deData: TcxDBDateEdit;
    cxGroupBox3: TcxGroupBox;
    cxGroupBox6: TcxGroupBox;
    gdItens: TcxGrid;
    gdItensDBTableView1: TcxGridDBTableView;
    colItensCodigo: TcxGridDBColumn;
    colItensDesc: TcxGridDBColumn;
    colItensQtd: TcxGridDBColumn;
    gdItensLevel1: TcxGridLevel;
    sqlCompras: TSQLDataSet;
    dspCompras: TDataSetProvider;
    cdsCompras: TClientDataSet;
    dsCompras: TDataSource;
    dsItens: TDataSource;
    cdsItens: TClientDataSet;
    dspItens: TDataSetProvider;
    sqlItens: TSQLDataSet;
    dsOperacoes: TDataSource;
    cdsOperacoes: TClientDataSet;
    cdsOperacoesCodigo: TIntegerField;
    cdsOperacoesDescricao: TStringField;
    dspOperacoes: TDataSetProvider;
    sqlOperacoes: TSQLDataSet;
    sqlOperacoesCodigo: TIntegerField;
    sqlOperacoesDescricao: TStringField;
    sqlProdutos: TSQLDataSet;
    IntegerField1: TIntegerField;
    StringField1: TStringField;
    dspProdutos: TDataSetProvider;
    cdsProdutos: TClientDataSet;
    IntegerField2: TIntegerField;
    StringField2: TStringField;
    dsProdutos: TDataSource;
    Panel1: TPanel;
    btnConsulta: TcxButton;
    cxButton4: TcxButton;
    cxButton2: TcxButton;
    sqlComprasSequencial: TIntegerField;
    sqlComprasData: TSQLTimeStampField;
    sqlComprasTipo: TIntegerField;
    cdsComprasSequencial: TIntegerField;
    cdsComprasData: TSQLTimeStampField;
    cdsComprasTipo: TIntegerField;
    cxDBLookupComboBox1: TcxDBLookupComboBox;
    sqlItensSeqLista: TIntegerField;
    sqlItensSequencial: TIntegerField;
    sqlItensProduto: TIntegerField;
    cdsItensSeqLista: TIntegerField;
    cdsItensSequencial: TIntegerField;
    cdsItensProduto: TIntegerField;
    sqlItensQtd: TFMTBCDField;
    cdsItensQtd: TFMTBCDField;
    procedure FormShow(Sender: TObject);
    procedure CarregaDataSets;
    procedure CarregaItens(iSeq:INteger);    
    procedure cdsComprasBeforePost(DataSet: TDataSet);
    procedure cdsComprasAfterPost(DataSet: TDataSet);
    procedure QueryBasica(iSeq:INteger);
    procedure dsComprasDataChange(Sender: TObject; Field: TField);
    procedure cdsItensAfterPost(DataSet: TDataSet);
    procedure cdsItensBeforePost(DataSet: TDataSet);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure cdsItensAfterDelete(DataSet: TDataSet);
    procedure btnConsultaClick(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure gdItensDBTableView1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    S:TStringList;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmEdListaCompra: TfrmEdListaCompra;

implementation
Uses M_Principal, Bib;
{$R *.dfm}

procedure TfrmEdListaCompra.FormShow(Sender: TObject);
begin
  S:=TStringList.Create;
  CarregaDataSets;
  QueryBasica(frmEdListaCompra.Tag);
end;

procedure TfrmEdListaCompra.cdsComprasBeforePost(DataSet: TDataSet);
begin
  if cdsCompras.State = dsInsert then Begin
    cdsCompras.FieldByName('Sequencial').asINteger:=CarregaSeq('ListaCompra','Sequencial');
  end;
end;

procedure TfrmEdListaCompra.cdsComprasAfterPost(DataSet: TDataSet);
begin
  cdsCompras.ApplyUpdates(0);
end;

procedure TfrmEdListaCompra.CarregaDataSets;
begin
  cdsOperacoes.Open;
  cdsProdutos.Open;
end;

procedure TfrmEdListaCompra.QueryBasica(iSeq:Integer);
begin
  S.Clear;
  S.Add('Select Sequencial,  Data, Tipo');
  S.Add('From ListaCompra');
  S.Add('Where Sequencial = ' + IntToStr(iSeq));
  with cdsCompras do Begin
    Close;
    CommandText:=S.text;
    Open;
    if iSeq > 0 then begin
      Edit;
    end else begin
      Insert;
    end;
  end;
end;

procedure TfrmEdListaCompra.dsComprasDataChange(Sender: TObject;
  Field: TField);
begin
  if cdsCompras.RecordCount > 0 then begin
    CarregaItens(frmEdListaCompra.Tag);
  end;
end;

procedure TfrmEdListaCompra.CarregaItens(iSeq: INteger);
begin
  S.Clear;
  S.Add('Select SeqLista, Sequencial, Produto,  Qtd');
  S.Add('From ItensListaCompra');
  S.Add('Where SeqLista = ' + IntToStr(iSeq));
  with cdsItens do Begin
    Close;
    CommandText:=S.text;
    Open;
  end;
end;

procedure TfrmEdListaCompra.cdsItensAfterPost(DataSet: TDataSet);
begin
  cdsItens.ApplyUpdates(0);
end;

procedure TfrmEdListaCompra.cdsItensBeforePost(DataSet: TDataSet);
begin
  if cdsItens.State = dsInsert then begin
    cdsItens.FieldByName('SeqLista').asINteger:=cdsCompras.FieldByName('Sequencial').asINteger;
    cdsItens.FieldByName('Sequencial').asINteger:=CarregaSeq('ItensListaCompra','Sequencial');
  end;
end;

procedure TfrmEdListaCompra.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if activecontrol <> gdItens Then begin
    if Key = #13 then begin
      Key := #0;
      Perform(WM_NEXTDLGCTL, 0, 0);
    end;
  end;
end;

procedure TfrmEdListaCompra.cdsItensAfterDelete(DataSet: TDataSet);
begin
  cdsItens.ApplyUpdates(0);
  cdsCompras.Refresh;  
end;

procedure TfrmEdListaCompra.btnConsultaClick(Sender: TObject);
begin
  if cdsCompras.State in [dsInsert,dsEdit] then begin
    cdsCompras.Post;
  end;
end;

procedure TfrmEdListaCompra.cxButton2Click(Sender: TObject);
begin
  if cdsCompras.State in [dsInsert,dsEdit] then begin
    cdsCompras.Cancel;
  end;
end;

procedure TfrmEdListaCompra.gdItensDBTableView1KeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if not (cdsCompras.State in [dsEdit,dsInsert]) then begin
    if (key=vk_INSERT) then begin
      colItensCodigo.FocusWithSelection;
      cdsItens.Insert;
    end else if (key=vk_DELETE) then begin
      cdsItens.Delete;
    end;
  end;
end;

end.
